#import "Level1Layer.h"

@implementation Level1Layer
@synthesize rowCount, columnCount;

-(id) init
{
    if( (self = [super init]) )
    {
        [self setRowCount:3];
        [self setColumnCount:4];
        
        CGSize winSize = [self contentSize];
        
        //Create the background image 
        CCSprite *background = [CCSprite spriteWithFile:@"Default.png"];
        [background setPosition:CGPointMake(winSize.width/2, winSize.height/2)];
        //[self addChild:background z:0];
	
    }
    
    return self;
}

-(void) dealloc
{
    [super dealloc];
}

#pragma mark Overrided Function
-(void) createSprites:(NSMutableArray *)numberArray wave:(int)wave
{
    CGSize winSize = [self contentSize];
    int gridWidth = winSize.width/columnCount;
    int gridHeight = winSize.height/rowCount;

    //[self setRemainSpriteCount:[arrGeneratedNumber count]];
    
	//Remove all Sprites object in Array first
	[spritesArray release];
	spritesArray = [[NSMutableArray alloc] initWithCapacity:[numberArray count]];

	//Shuffle the answer array
    NSMutableArray *shuffledArray = [self shuffleArray:[ [numberArray mutableCopy] autorelease]];
	
	int i=0;
    //Create sprite
    for(int row=0; row<rowCount; row++)
    {
        for(int column=0; column< columnCount; column++)
        {
            int num = [[shuffledArray objectAtIndex:i] intValue];
            i++;

            SquareSprite *sprite = (SquareSprite*)[self createSpriteWithType:kSPRITE_TYPE_SQUARE number:num];
            sprite.position = ccp(column * gridWidth + gridWidth/2,
                                   row * gridHeight + gridHeight/2);
            [sprite setDelegate:self];
            [self addChild:sprite];
            [self.spritesArray addObject:sprite];            
        }
    }

}

-(void) startLevel
{
	
}

-(void) startWave:(int)wave
{
	//Ask Sprite to start action
	for(int i=0; i< [self.spritesArray count]; i++)
	{
		GeneralSprite *sprite = [self.spritesArray objectAtIndex:i];
		[sprite startActionWithWave:wave];
	}
}

-(void) willCompleteLevel
{
	
}

-(void) willCompleteWave
{
	
}

-(void) startPerformCorrectAnswer:(GeneralSprite *)sprite
{
	//TODO: Perform any Layer correct action
	
	//Perform Sprite Action
	[sprite startCorrectEffect];
}

-(void) startPerformIncorrectAnswer:(GeneralSprite *)sprite
{
	//TODO: Perform any Layer correct action
	
	//Perform Sprite Action
	[sprite startIncorrectEffect];
}

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	CCLOG(@"Touch Began");
}

-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
	CCLOG(@"Touch End");
}

-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
	CCLOG(@"Touch Moved");
}

/*
#pragma mark SquareSpriteDelegare
-(void) squareSpriteTouchBegan:(SquareSprite *)sender
{
    bool bIsCorrect = [delegate checkAnswer:(int)sender.number];
    
    if(bIsCorrect)
    {
        [sender startCorrectEffect];
    }
    else
    {
        [delegate deductTime];
        [sender startIncorrectEffect];
    }
}

-(void) squareSpriteTouchMoved:(SquareSprite *)sprite location:(CGPoint)location
{
    
}

-(void) squareSpriteTouchEnd:(SquareSprite *)sprite
{
    
}
-(void) squareStartCorrectEffect:(SquareSprite *)sender
{
    
}

-(void) squareFinishCorrectEffect:(SquareSprite *)sender
{
    CGPoint pos = sender.position;
    
    //Remove the Sprite if correct 
    [self removeSprite:sender];
    
    BOOL bIsWaveCompleted = [delegate isWaveComplete];
    if(bIsWaveCompleted)
    {
        //Check if level comepleted
        BOOL bIsLevelCompleted = [delegate isLevelComplete];

        if(bIsLevelCompleted)
        {
            [delegate pauseTimer];
            [self showGuGu:pos];
        }
        else
        {
            //Another wave
            [delegate waveCompleted];
        }
    }
}

-(void) squareStartIncorrectEffect:(SquareSprite *)sender
{
    
}

-(void) squareFinishIncorrectEffect:(SquareSprite *)sender
{    
}
*/
@end
